﻿using System;
using System.Collections.Generic;
using System.Globalization;
using InhollandAir.auxiliary;
using PWDTK_DOTNET45;

namespace InhollandAir
{
    public class HashComparison
    {
        private static int _iterations;
        private const int SaltSize = PWDTK.cDefaultSaltLength;
 
        public static Dictionary<string, string> GenerateRandomPasswordAndHash(string password)
        {
            Byte[] salt = PWDTK.GetRandomSalt(SaltSize);
            _iterations = int.Parse(CryptoRandom.GetIterationCount());

            
            return new Dictionary<string, string> { 
                { "salt", PWDTK.HashBytesToHexString(salt)}, 
                { "hash", PWDTK.HashBytesToHexString(PWDTK.PasswordToHash(salt, password, _iterations))},
                { "iteration", _iterations.ToString(CultureInfo.InvariantCulture)}
            };
        }

        public static string CheckHashMatch(string password, string username)
        {
            var saltInfo = Sql.GetSaltAndIterationByUserId(username);
            _iterations = int.Parse(saltInfo[0]);
            string salt = saltInfo[1];
            string hash = Sql.GetHashedPasswordByUsername(username);

            return PWDTK.HashBytesToHexString(PWDTK.PasswordToHash(PWDTK.HashHexStringToBytes(salt), password, _iterations));

        }
    }
}